//
//  CMDBAssociateCollectionView.swift
//  CMDB
//
//  Created by 王磊 on 09/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit
extension CMDBAssociateCollectionView {
    
    override static func getInstance() -> CMDBAssociateCollectionView {
        
        let flowLayout = CMDBAssociateFlowLayout()
        
        return CMDBAssociateCollectionView.getInstance(flowLayout)
    }
    static func getInstance(flowLayout: UICollectionViewFlowLayout) -> CMDBAssociateCollectionView {
        
        return CMDBAssociateCollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
    }
}
class CMDBAssociateCollectionView: UICollectionView {
    
    private var dataArray: Array<AnyObject> = []
    
    internal weak var associateDelegate: CMDBAssociateFooterViewDelegate?
    
    private var selectWorks: NSMutableArray = NSMutableArray()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        commitInitOwnProperties()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension CMDBAssociateCollectionView {
    
    internal func updateData(arr: Array<AnyObject>) {
        
        dataArray += arr
        
        reloadData()
    }
}

private let cellIdentifier: String = "CMDBAssociateCollectionViewCell"
extension CMDBAssociateCollectionView {
    
    internal func commitInitOwnProperties() {
        
        showsVerticalScrollIndicator = false
        
        showsHorizontalScrollIndicator = false
        
        bounces = true
        
        dataSource = self
        
        delegate = self
        
        registerClass(CMDBAssociateCollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
        
        registerClass(CMDBAssociateHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CMDBAssociateHeaderView")
        
        registerClass(CMDBAssociateFooterView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "CMDBAssociateFooterView")
        //
        clipsToBounds = false
        
        backgroundColor = UIColor.clearColor()
    }
}

// MARK: data source
extension CMDBAssociateCollectionView: UICollectionViewDataSource {}

extension CMDBAssociateCollectionView {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CMDBAssociateCollectionViewCell
        
        cell.indexPath = indexPath
        //
        cell.updateData(dataArray[indexPath.row])
        //
        //        cell.addTarget(self, action: #selector(cellAction))
        
        return cell
    }
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionElementKindSectionHeader {
            
            let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "CMDBAssociateHeaderView", forIndexPath: indexPath) as! CMDBAssociateHeaderView
            
            return header
        } else {
            
            let footer = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "CMDBAssociateFooterView", forIndexPath: indexPath) as! CMDBAssociateFooterView
            
            footer.associateDelegate = self
            
            return footer
        }
    }
}

// MARK: delegate

extension CMDBAssociateCollectionView: CMDBAssociateFooterViewDelegate {}

extension CMDBAssociateCollectionView {
    
    func associateWorks() {
        
        guard let associateDelegate = associateDelegate else {
            return
        }
        
        associateDelegate.associateWorks()
    }
}

extension CMDBAssociateCollectionView: UICollectionViewDelegate {}

extension CMDBAssociateCollectionView: UICollectionViewDelegateFlowLayout {}

extension CMDBAssociateCollectionView {
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        let works: CMDBWorksBean = dataArray[indexPath.item] as! CMDBWorksBean
        
        let cell: CMDBAssociateCollectionViewCell = cellForItemAtIndexPath(indexPath)
            as! CMDBAssociateCollectionViewCell
        if works.select {
            
            works.select = false
            
            cell.selectWorks(false)
            
            selectWorks.removeObject(works)
        } else {
            
            selectWorks.addObject(works)
            
            works.select = true
            
            cell.selectWorks(true)
        }
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSizeMake(Associate_Item_Width , Associate_Item_Height)
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        
        return CGSizeMake(CGRectGetWidth(collectionView.bounds), Associate_Section_Header_Height)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        
        return CGSizeMake(CGRectGetWidth(collectionView.bounds), Associate_Section_Footer_Height)
    }
}
extension CMDBAssociateCollectionView {
    
    internal func getSelectedWorksId() -> Array<String> {
        
        var arr: Array<String> = []
        
        _ = selectWorks.map {
            
            arr += [($0 as! CMDBWorksBean).wid]
        }
        return arr
    }
}

